package com.example.passingdatatointents;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.passingdatatointents.data.*;

public class MainActivity extends ListActivity {

    private static final int REQUEST_CODE = 100;
    List<Flower> flowers= new FlowerData().getFlowers();
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ArrayAdapter<Flower> adapter = new ArrayAdapter<Flower>(this, android.R.layout.simple_list_item_1,flowers);
        setListAdapter(adapter);
        
        /*LinearLayout layout = (LinearLayout) findViewById(R.id.listLayout);
        
        for (final Flower flower : flowers) {
			Button button = new Button(this);
			button.setText(flower.flowerName);
			layout.addView(button); 
			button.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(MainActivity.this,MainDetail.class);
					intent.putExtra("flowerName",flower.flowerName);
					intent.putExtra("imageResource",flower.imageResource);
					intent.putExtra("instructions",flower.instructions);
					startActivityForResult(intent, REQUEST_CODE);
				}
			});
		}*/
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
			String flowerName = data.getStringExtra("flowerName");
			String action = data.getStringExtra("action");
			if(action.equals("add")){
				Toast.makeText(this, "adding "+flowerName+" to cart", Toast.LENGTH_LONG).show();
			}
		}
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Flower flower = flowers.get(position);
		Intent intent = new Intent(this,MainDetail.class);
		intent.putExtra("flowerName",flower.flowerName);
		intent.putExtra("imageResource",flower.imageResource);
		intent.putExtra("instructions",flower.instructions);
		startActivityForResult(intent, REQUEST_CODE);
	}
}
